#!/bin/sh

e2e_test_install() {
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set cluster.postgresServices.primary.enabled="false" \
    --set cluster.postgresServices.replicas.enabled="false"
}

e2e_test() {
  run_test "Creating a cluster with services disabled" services_disabled

  run_test "Creating a cluster with services enabled with specific annotations" services_annotations

  run_test "Creating a cluster with services as node port" node_port_services

  run_test "Creating a cluster with services as load balancer" load_balancer_services

  run_test "Creating a cluster with services using externalIPs" using_external_IPS
}

services_disabled() {
  if kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" > /dev/null 2>&1
  then
    fail "Primary service was created"
  else
    success "Primary service was not created"
  fi

  if kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME"-"primary" > /dev/null 2>&1
  then
    fail "Primary service was created"
  else
    success "Primary service was not created"
  fi

  if kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME"-"replicas" > /dev/null 2>&1
  then
    fail "Replica service was created"
  else
    success "Replica services was not created"
  fi

  remove_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
}

services_annotations() {
  local PRIMARY_RANDOM_VALUE="$(random_string)"
  local REPLICA_RANDOM_VALUE="$(random_string)"

  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set cluster.postgresServices.primary.enabled="true" \
    --set cluster.postgresServices.replicas.enabled="true" \
    --set cluster.metadata.annotations.primaryService.primary-annotation="$PRIMARY_RANDOM_VALUE" \
    --set cluster.metadata.annotations.replicasService.replica-annotation="$REPLICA_RANDOM_VALUE"

  SERVICE_ANNOTATION="primary-annotation"
  wait_until check_service_annotation "" "$PRIMARY_RANDOM_VALUE"

  SERVICE_ANNOTATION="replica-annotation"
  wait_until check_service_annotation "replicas" "$REPLICA_RANDOM_VALUE"

  remove_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
}

node_port_services() {
  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set cluster.postgresServices.primary.enabled="true" \
    --set cluster.postgresServices.replicas.enabled="true" \
    --set cluster.postgresServices.primary.type="NodePort" \
    --set cluster.postgresServices.replicas.type="NodePort"

  wait_until kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME"
  wait_until kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-primary"
  wait_until kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-replicas"

  local PRIMARY_SERVICE_TYPE
  PRIMARY_SERVICE_TYPE="$(kubectl get services -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o jsonpath='{.spec.type}')"
  local OLD_PRIMARY_SERVICE_TYPE
  OLD_PRIMARY_SERVICE_TYPE="$(kubectl get services -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-primary" -o jsonpath='{.spec.type}')"
  local REPLICA_SERVICE_TYPE
  REPLICA_SERVICE_TYPE="$(kubectl get services -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-replicas" -o jsonpath='{.spec.type}')"

  assert_string_equal "NodePort" "$PRIMARY_SERVICE_TYPE"
  assert_string_equal "ExternalName" "$OLD_PRIMARY_SERVICE_TYPE"
  assert_string_equal "NodePort" "$REPLICA_SERVICE_TYPE"

  remove_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
}

load_balancer_services() {
  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set cluster.postgresServices.primary.enabled="true" \
    --set cluster.postgresServices.replicas.enabled="true" \
    --set cluster.postgresServices.primary.type="LoadBalancer" \
    --set cluster.postgresServices.replicas.type="LoadBalancer"

  wait_until kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME"
  wait_until kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-primary"
  wait_until kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-replicas"

  local PRIMARY_SERVICE_TYPE
  PRIMARY_SERVICE_TYPE="$(kubectl get services -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o jsonpath='{.spec.type}')"
  local OLD_PRIMARY_SERVICE_TYPE
  OLD_PRIMARY_SERVICE_TYPE="$(kubectl get services -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-primary" -o jsonpath='{.spec.type}')"
  local REPLICA_SERVICE_TYPE
  REPLICA_SERVICE_TYPE="$(kubectl get services -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-replicas" -o jsonpath='{.spec.type}')"

  assert_string_equal "LoadBalancer" "$PRIMARY_SERVICE_TYPE"
  assert_string_equal "ExternalName" "$OLD_PRIMARY_SERVICE_TYPE"
  assert_string_equal "LoadBalancer" "$REPLICA_SERVICE_TYPE"

  remove_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
}

using_external_IPS() {
  local PRIMARY_EXTERNAL_IP="10.0.1.10"
  local REPLICA_EXTERNAL_IP="10.0.2.10"
  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE" \
    --set cluster.postgresServices.primary.enabled="true" \
    --set cluster.postgresServices.replicas.enabled="true" \
    --set cluster.postgresServices.primary.externalIPs[0]="$PRIMARY_EXTERNAL_IP" \
    --set cluster.postgresServices.replicas.externalIPs[0]="$REPLICA_EXTERNAL_IP"

  wait_until kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME"
  wait_until kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-primary"
  wait_until kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-replicas"

  local PRIMARY_SERVICE_EXTERNAL_IP
  PRIMARY_SERVICE_EXTERNAL_IP="$(kubectl get services -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o jsonpath='{.spec.externalIPs[0]}')"
  local DEPRECATED_PRIMARY_SERVICE_EXTERNAL_IP
  DEPRECATED_PRIMARY_SERVICE_EXTERNAL_IP="$(kubectl get services -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-primary" -o jsonpath='{.spec.externalIPs[0]}')"
  local REPLICA_SERVICE_EXTERNAL_IP
  REPLICA_SERVICE_EXTERNAL_IP="$(kubectl get services -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-replicas" -o jsonpath='{.spec.externalIPs[0]}')"

  assert_string_equal "$PRIMARY_EXTERNAL_IP" "$PRIMARY_SERVICE_EXTERNAL_IP"
  assert_string_equal "$REPLICA_EXTERNAL_IP" "$REPLICA_SERVICE_EXTERNAL_IP"
  assert_string_equal "" "$DEPRECATED_PRIMARY_SERVICE_EXTERNAL_IP"

  remove_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
}

